
from functools import partial
from src.feature_extract.metadata import FileInfo
from src.feature_extract.property_factory import FilePropertyFactory
from src.tasks import run_author_perspertive_task,run_author_attribution_task,AuthorPerspertiveModelAdapter,AuthorAttributionModelAdapter
from src.tasks.util import log


import src.feature_extract.property_selector as property_selector

def run_label_test_for_perspective_task(source_dirs,extend_data_paths,lang,min_number):

    choose_social_labels=["in_love_time","cet6","exp_province","birth_province","ranking","religious_belief","sex"]
    choose_psy_labels=["emotional","freshness","kindness","optimistic","responsibility"]

    text2label_selector={}

    for e in choose_social_labels:
        text2label_selector[e]=partial(property_selector.social_label_selector_handle,e)

    for e in choose_psy_labels:
        text2label_selector[e]=partial(property_selector.psy_label_selector_handle,e)

    all_labels=choose_social_labels+choose_psy_labels
    for i,label_name in enumerate(all_labels):
        show_summary=(i==0)
        try:
            model=AuthorPerspertiveModelAdapter.default_model()
            run_author_perspertive_task(source_dirs,extend_data_paths,model,lang,text2label_selector[label_name],min_number,show_summary,True,f'{label_name}')
        except AssertionError as e:
            log(f"{label_name}: can't predict. reason: train sample not enough")



test_dict={
    'java':['data/java/reportOriginFeature'],
    'cpp':["data/cpp/resultjson_mimaxue","data/cpp/resultjson_sf"]
}

# def default_property_selector(file_info:FileInfo):
#     file_property=FilePropertyFactory.create_property(file_info)
#     r=file_property.ratio_property
#     return [r.digit_ratio,r.lower_ratio,r.upper_ratio,r.punctuation_ratio,r.blank_line_ratio,r.on_line_before_open_brance_ratio,r.tab_indent_ratio,r.literal_ratio]

# choose_lang='java'

# run_author_attribution_task(test_dict[choose_lang],AuthorAttributionModelAdapter.default_model(),choose_lang,10)

choose_lang='cpp'

run_author_attribution_task(test_dict[choose_lang],AuthorAttributionModelAdapter.default_model(),choose_lang,min_number=5)


run_label_test_for_perspective_task(test_dict[choose_lang],['data/psychology/psy.json'],choose_lang,2)

